package com.fqr;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.log4j.Logger;

public class EmailUtil {

	private Logger log = Logger.getLogger(EmailUtil.class);
	
	private int emailRetry = 0;
	private String smtpServer;
	private int smtpPort;
	private String user;
	private String pass;
	private boolean ssl;
	private String sslPort;
	
	
	private int maxEmailRetry = 0;

	public EmailUtil() {
		try {
			String emailFileName = "email.properties";
			Properties p = new Properties();
			p.load(EmailUtil.class.getClassLoader().getResourceAsStream(emailFileName));

			maxEmailRetry = Integer.parseInt(p.getProperty("EMAIL.RETRY"));
			smtpServer = p.getProperty("EMAIL.SMTP.HOST");
			smtpPort = Integer.parseInt(p.getProperty("EMAIL.SMTP.PORT"));
			user = p.getProperty("EMAIL.SMTP.USER");
			pass = p.getProperty("EMAIL.SMTP.PASS");
			ssl = Boolean.parseBoolean(p.getProperty("EMAIL.SMTP.SSL", "false"));
			sslPort = p.getProperty("EMAIL.SMTP.SSL.PORT");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public boolean sendEmail(EmailInfo eObj) {
		boolean emailSendOk = true;
		try {
			emailRetry++;
			List<String> emailRecipientList = eObj.getEmailrecAdd();
			String emailFrom = eObj.getEmailfromAdd();
			String emailSubject = eObj.getEmailSubject();
			String emailMessage = eObj.getEmailMessage();
			File fileAttach = eObj.getEmailfileAttachment();
			String attachName = eObj.getEmailFileAttachmentName();
			// Create the email message
			MultiPartEmail email = new MultiPartEmail();
			if (fileAttach != null) {
				log.info("File to attach: " + fileAttach.getAbsolutePath());
				EmailAttachment attachment = new EmailAttachment();
				attachment.setPath(fileAttach.getAbsolutePath());
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				attachment.setDescription(emailSubject);
				attachment.setName(attachName == null ? fileAttach.getName() : attachName);
				// add the attachment
				email.attach(attachment);
			}
			email.setHostName(smtpServer);
			email.setSmtpPort(smtpPort);
			
			if(user != null && user.trim().length() > 0) {
				email.setAuthentication(user, pass);
			}
			
			if(ssl) {
				email.setSSL(true);
				email.setSslSmtpPort(sslPort);
			}
			
			for (int i = 0; i < emailRecipientList.size(); i++) {
				String emailRecipient = emailRecipientList.get(i);
				email.addTo(emailRecipient);
			}
			email.setFrom(emailFrom);
			email.setSubject(emailSubject);
			email.setMsg(emailMessage);

			// send the email
			String status = email.send();
			
			log.info(status + " - sending email " + emailSubject);
			
			emailSendOk = true;
		} catch (EmailException e) {
			if (emailRetry > maxEmailRetry) {
				emailSendOk = false;
				e.printStackTrace();
			} else {
				log.error("Exception in sending email. Retrying: " + emailRetry + " time");
				sendEmail(eObj);
			}
		}

		return emailSendOk;
	}

	public static void main(String[] args) {
		EmailUtil emailUtil = new EmailUtil();
		EmailInfo email = new EmailInfo();
		email.setEmailfromAdd("yaxinwang@hotmail.com");
		ArrayList<String> to = new ArrayList<String>();
		to.add("yaxin.wang@gmail.com");
		email.setEmailrecAdd(to);
		email.setEmailfileAttachment(new File("WCG_Swaptions.csv"));
		email.setEmailSubject("test");
		email.setEmailMessage("test email");

		emailUtil.sendEmail(email);
	}
}
